var fsExtra = require('fs-extra');
// fsExtra.move(`../server/public/master`, `../server/public/master2`, function(err) {
//     if (err) return console.error(err)
//     console.log("move file success!")
// });
import {argv as args} from 'yargs';

var shell = require("shelljs");

var curVersion = new Date().getTime();

class Exec {
    constructor() {
    }

    pull() {
        return new Promise((resolve, reject) => {
                shell.exec("git pull", () => {
                    resolve();
                })
            }
        )
    }

    //普通编译，不压缩
    by() {
        return new Promise((resolve, reject) => {
                shell.exec("npm run build", () => {
                    resolve();
                })
            }
        )
    }

    //编译后压缩，并且更换所有链接的publicPath为cdn地址
    bycdn() {
        return new Promise((resolve, reject) => {
                shell.exec("npm run build -- cdn", () => {
                    resolve();
                })
            }
        )
    }

    //更换测试环境的版本号
    tvs() {
        return new Promise((resolve, reject) => {
                shell.exec(`curl -X POST http://vcg-design.shijue.me/n/changeVersion -d "version=${curVersion}"`, function () {
                    console.log('\nchange vcg-design.shijue.me nodejs version success')
                });
                shell.exec(`curl -X POST http://node.shijue.me/admin_config/lyby_changeVersion -d "version=${curVersion}&indexFlag=www_vcg_test"`, function () {
                    console.log('\nchange vcg-design.shijue.me java version success')
                });
                resolve();
            }
        )
    }

    vs() {
        return new Promise((resolve, reject) => {
                shell.exec(`curl -X POST http://shijue.me/n/changeVersion -d "version=${curVersion}"`, function () {
                    console.log('\nchange shijue.me nodejs version success')
                });
                shell.exec(`curl -X POST http://node.shijue.me/admin_config/lyby_changeVersion -d "version=${curVersion}&indexFlag=www_vcg_online"`, function () {
                    console.log('\nchange shijue.me java version success')
                })
                resolve();
            }
        )
    }
}
let allPromise = [];
let execObj = new Exec();
args._.forEach((key) => {
    allPromise.push(execObj[key]);
});

let digui = function (apfn) {
    if (apfn) {
        apfn().then(()=>{
            digui(allPromise.shift())
        })
    }
};
digui(allPromise.shift());

// shell.exec("git pull", function () {
//     shell.exec("npm run server", function () {
//         var curVersion = new Date().getTime();
//         shell.exec(`curl -X POST http://vcg-design.shijue.me/n/changeVersion -d "version=${curVersion}"`, function () {
//             console.log('\nvcg-design.shijue.me nodejs version success')
//         });
//         shell.exec(`curl -X POST http://node.shijue.me/admin_config/lyby_changeVersion -d "version=${curVersion}&indexFlag=www_vcg_test"`, function () {
//             console.log('\nvcg-design.shijue.me java version success')
//         });
//
//         shell.exec(`curl -X POST http://shijue.me/n/changeVersion -d "version=${curVersion}"`, function () {
//             console.log('\nshijue.me nodejs version success')
//         });
//         shell.exec(`curl -X POST http://node.shijue.me/admin_config/lyby_changeVersion -d "version=${curVersion}&indexFlag=www_vcg_online"`, function () {
//             console.log('\nshijue.me java version success')
//         })
//     });
// })